package com.jzg.jcptadmin.presenter;

import com.jzg.jcptadmin.base.BasePresenter;
import com.jzg.jcptadmin.constant.Constant;
import com.jzg.jcptadmin.data.DataManager;
import com.jzg.jcptadmin.data.vo.Table;
import com.jzg.jcptadmin.data.vo.ValuationDetailData;
import com.jzg.jcptadmin.viewinterface.VinResultInterface;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import rx.Observable;
import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by wangyd on 15/12/29.
 * VIN码查询结果逻辑
 */
public class VinResultPresenter extends BasePresenter<VinResultInterface> {

    private final DataManager mDataManager;
    private Subscription mSubscription;
    private VinResultInterface mView;

    private String curBsx = "全部";

    private String curPl = "全部";

    //当前vin查询的列表信息
    private List<Table> table;
    //当前排量信息
    private List<String> plList;
    //当前变速箱信息
    private List<String> bsxList;

    public VinResultPresenter(DataManager instance) {
        super();
        mDataManager = instance;
    }

    @Override
    public void attachView(VinResultInterface mvpView) {
        super.attachView(mvpView);
        this.mView = mvpView;
    }

    @Override
    public void detachView() {
        super.detachView();
        if (mSubscription != null) mSubscription.unsubscribe();
    }

    /**
     * 筛选vin码结果列表
     */
    public void filterVinList() {
        final List<Table> filterTable = new ArrayList<>();
        mSubscription = Observable.from(table).filter(new Func1<Table, Boolean>() {
            @Override
            public Boolean call(Table table) {
                Logger.i("curBsx is " + curBsx + ",curPl is " + curPl);
                //当两个选项都等于全部的时候返回所有数据
                if (curBsx.equals("全部") && curPl.equals(
                        "全部")) {
                    return true;
                }
                //当排量等于全部时返回当前变速箱选中的数据
                else if ("全部".equals(curPl)) {
                    return table.getBsq().equals(curBsx);
                }
                //当变速箱等于全部时返回当前排量选中的数据
                else if ("全部".equals(curBsx)) {
                    return table.getPl().equals(curPl);
                }
                //其他情况返回和当前选中项变速箱和排量都匹配的数据
                else {
                    return table.getBsq().equals(curBsx) && table.getPl().equals(curPl);
                }
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<Table>() {
            @Override
            public void onCompleted() {
                Logger.i("onCompleted");
                mView.showVinResult(filterTable);
            }

            @Override
            public void onError(Throwable e) {
                if(e!=null&& mView!=null){

                    mView.showError(e.toString());
                }
            }

            @Override
            public void onNext(Table table) {
                Logger.i("onNext is " + table.toString());
                filterTable.add(table);
            }
        });
    }

    public void setBsxList(List<String> bsxList) {
        bsxList.add(0, "全部");
        this.bsxList = bsxList;
    }

    public void setTable(List<Table> table) {
        this.table = table;
    }

    public void setPlList(List<String> plList) {
        plList.add(0, "全部");
        this.plList = plList;
    }

    public void setCurBsx(String curBsx) {
        this.curBsx = curBsx;
    }

    public void setCurPl(String curPl) {
        this.curPl = curPl;
    }

    public List<String> getBsxList() {
        return bsxList;
    }

    public List<String> getPlList() {
        return plList;
    }

    public void loadEstimate() {
        Map<String, String> params = mView.getEstimateParams();
        if (!checkSubmitInfo(params)) return;
        mSubscription = mDataManager.LoadValuationDetailData(params).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Observer<ValuationDetailData>() {
            @Override
            public void onCompleted() {
                mView.dismissDialog();
            }

            @Override
            public void onError(Throwable e) {
                if (e != null&& mView!=null) {
                    mView.showError(e.getMessage());
                }
            }

            @Override
            public void onNext(ValuationDetailData valuationDetailData) {
                if (valuationDetailData.getStatus() == Constant.SUCCESS)
                    mView.loadValuationDetail(valuationDetailData);
                else
                    mView.showError(valuationDetailData.getMsg());
            }
        });
    }
}
